<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称" prop="templateName">
        <el-input v-model="queryParams.templateName" placeholder="请输入模板名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="检查项" prop="inspectionItem">
        <el-input
          v-model="queryParams.inspectionItem"
          placeholder="请输入检查项"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- <el-form-item label="工序ID" prop="processId"> 
        <el-input
          v-model="queryParams.processId"
          placeholder="请输入工序ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- <el-form-item label="工序编码" prop="processCode">
        <el-input
          v-model="queryParams.processCode"
          placeholder="请输入工序编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="模板编码" prop="templateCode">
        <el-input v-model="queryParams.templateCode" placeholder="请输入模板编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="产品id" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['data:selfTest:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['data:selfTest:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['data:selfTest:delete']"
          >删除</el-button
        >
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['data:selfTest:export']"
          >导出</el-button
        >
      </el-col>-->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="50" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="产品检测类型" align="center" prop="productType">
        <template slot-scope="scope">
          <span>
            {{
              scope.row.productType === "DC"
                ? "直流"
                : scope.row.productType === "AC"
                ? "交流"
                : scope.row.productType === "VM"
                ? "车载"
                : scope.row.productType === "PCBA"
                ? "PCBA"
                : scope.row.productType === "WR"
                ? "线材"
                : "--"
            }}
          </span>
        </template>
</el-table-column>-->
      <!-- <el-table-column label="检查项" align="center" prop="inspectionItem" /> -->
      <!-- <el-table-column label="工序ID" align="center" prop="processId" /> -->
      <!-- <el-table-column label="工序编码" align="center" prop="processCode" />
      <el-table-column label="工序名称" align="center" prop="processName" />-->
      <el-table-column label="模板编码" align="center" prop="templateCode" />
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="所属产品类型" align="center" prop="productType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_product_type" :value="scope.row.productType" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="产品id" align="center" prop="productId" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['data:selfTest:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['data:selfTest:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工位自检基础对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="模板编码" prop="templateCode">
          <el-input v-model="form.templateCode" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入产品名称" />
          <!-- <el-select v-model="form.productType" placeholder="请选择检测类型">
            <el-option
              v-for="item in productTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>-->
        </el-form-item>
        <el-form-item label="所属产品类型" prop="productType">
          <el-select v-model="form.productType" placeholder="请选择">
            <el-option v-for="dict in dict.type.mes_product_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>-->
        <!-- <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <div v-if="optType == 'edit'">
        <!-- <el-tag size="large" style="font-size: 20px; margin-bottom: 10px; margin-top: 10px">自检项目明细</el-tag> -->
        <el-tabs type="border-card">
          <el-tab-pane label="自检项目明细">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addAnalysis"
                  >新增</el-button>
              </el-col>
              <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExportAnalysis"
              v-hasPermi="['mes:pro:proroute:export']"
            >导出</el-button>
              </el-col>-->
            </el-row>
            <el-table v-loading="loadinga" :data="itemList" height="30vh">
              <el-table-column label="序号" align="center" width="50px">
                <template slot-scope="scope">
                  <span>{{ scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="检测项目" align="center" prop="checkItem" />
              <el-table-column label="所属工序" align="center" prop="workSequenceName" />
              <el-table-column label="检验类型" align="center" prop="checkTypeName" />
              <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-edit" @click="editAnalysis(scope.row)">修改</el-button>
                  <el-button size="mini" type="text" icon="el-icon-delete" @click="delDetail(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="关联产品">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addProduct"
                  >新增</el-button>
              </el-col>
              <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExportAnalysis"
              v-hasPermi="['mes:pro:proroute:export']"
            >导出</el-button>
              </el-col>-->
            </el-row>
            <el-table v-loading="loadingb" :data="productList" height="30vh">
              <el-table-column label="序号" align="center" width="50px">
                <template slot-scope="scope">
                  <span>{{ scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="BOM版本" align="center" prop="bomItemCode" />
              <el-table-column label="产品编码" align="center" prop="productCode" />
              <el-table-column label="产品名称" align="center" prop="productName" />
              <el-table-column label="规格型号" align="center" prop="productSpec" />

              <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-delete"
                    @click="delProduct(scope.row, scope.$index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <DetailList v-if="openDetail" ref="detailsQuery" @parentList="getAnalysis"></DetailList>
    <!-- <ProductDetail v-if="openProduct" ref="detailsProduct" @parentList="getProduct"></ProductDetail> -->
    <ItemSelect ref="itemSelect1" @onSelected="onItemSelected"> </ItemSelect>
  </div>
</template>

<script>
import {
  listInspection,
  getInspection,
  delInspection,
  addInspection,
  updateInspection,
  processList,
  listCheckConfDetail,
  delCheckConfDetail,
  getProductInfoList,
  delStationCheckConf,
} from "@/api/mes/pro/inspection";
import DetailList from "./detailList.vue";
// import ProductDetail from "./productDetail.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
export default {
  name: "Inspection",
  components: { DetailList, ItemSelect },
  dicts: ["mes_product_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      loadinga: false,
      loadingb: false,
      // 选中数组
      ids: [],
      itemList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工位自检基础表格数据
      inspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      openProduct: false,
      optType: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productType: null,
        inspectionItem: null,
        processId: null,
        processCode: null,
        processName: null,
        productCode: null,
        productId: null,
        templateName: null,
        templateCode: null,
      },
      // 表单参数
      form: {},
      productTypes: [
        {
          value: "DC",
          label: "直流",
        },
        {
          value: "AC",
          label: "交流",
        },
        {
          value: "VM",
          label: "车载",
        },
        {
          value: "PCBA",
          label: "PCBA",
        },
        {
          value: "WR",
          label: "线材",
        },
      ],
      //工序编码、工序名称
      processCodeList: [],

      // 表单校验
      rules: {},
      productList: [],
    };
  },
  created() {
    this.getList();
    this.getprocessList();
    // this.getProduct();
  },
  methods: {
    //获取工序编码list
    getprocessList() {
      processList().then((res) => {
        console.log(res, "工序编码list");
        this.processCodeList = res.rows;
      });
    },
    /** 查询工位自检基础列表 */
    getList() {
      this.loading = true;
      listInspection(this.queryParams).then((response) => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productType: null,
        inspectionItem: null,
        processId: null,
        processCode: null,
        processName: null,
        productCode: null,
        productId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        templateName: null,
        templateCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      // this.queryParams.roleIdList = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.optType = "add";
      this.title = "添加工位自检基础";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.optType = "edit";
      this.title = "修改工位自检基础";
      const id = row.id || this.ids;
      this.getAnalysis();
      this.getProduct();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.stationCheckConfList = this.productList
            console.log(this.form, "修改的form");
            updateInspection(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    //物料选择弹出框
    onItemSelected(obj) {
      console.log(obj, "obj");
      //把itemCode改成productCode
      obj.forEach((item) => {
        item.productCode = item.itemCode;
        item.productName = item.itemName;
        item.productSpec = item.specification;
        // item.itemId = item.itemId;
      });
      //循环判断
      if (obj != undefined && obj != null) {
        let allboor = false;
        for (const productItem of this.productList) {
          for (const objItem of obj) {
            if (objItem.productCode === productItem.productCode) {
              this.$modal.msgError("该物料已添加");
              return allboor = false;
            } else {
              // 如果子组件传过来的item和现有的item不相同则进行拼接
              allboor = true;
            }
          }
        }
        if (allboor || this.productList.length == 0) {
          if (this.productList.length == 0) {
            this.productList = obj;
          } else {
            this.productList = this.productList.concat(obj);
          }
          this.$modal.msgSuccess("新增成功");
        }
      }
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除工位自检基础编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInspection(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/inspection/export",
        {
          ...this.queryParams,
        },
        `inspection_${new Date().getTime()}.xlsx`
      );
    },
    addAnalysis() {
      this.openDetail = true;
      this.$nextTick(() => {
        this.$refs.detailsQuery.init(this.form, {}, "add");
        //清空表单
        this.$refs.detailsQuery.reset();
      });
    },
    addProduct() {
      this.openProduct = true;
      this.$nextTick(() => {
        this.$refs.itemSelect1.showFlag = true;
      });
    },
    editAnalysis(row) {
      this.openDetail = true;
      this.$nextTick(() => {
        this.$refs.detailsQuery.init(this.form, row, "edit");
      });
    },
    // 行表列表、
    getAnalysis() {
      this.loadinga = true;
      let params = {
        checkTemplateId: this.form.id,
      };
      listCheckConfDetail(this.form.id).then((response) => {
        this.itemList = response.data;
        this.loadinga = false;
      });
    },
    // 产品列表
    getProduct() {
      this.loadinga = true;
      let params = {
        checkTemplateId: this.form.id,
      };
      getProductInfoList(this.form.id).then((response) => {
        console.log(response, '关联产品的查询res');
        this.productList = response.data;
        this.loadinga = false;
      });
    },

    delDetail(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除检测项目为"' + row.checkItem + '"的数据项？')
        .then(function () {
          return delCheckConfDetail(id);
        })
        .then(() => {
          this.getAnalysis();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    editProduct(row) {
      this.openProduct = true;
      this.$nextTick(() => {
        this.$refs.detailsProduct.init(this.form, row);
      });
    },
    delProduct(row, index) {
      console.log(row, 'row', index);
      const id = row.id;
      this.$modal.confirm('是否确认删除检测项目为"' + row.productName + '"的数据项？').then(() => {
        // return delStationCheckConf(id);
        this.productList.splice(index, 1)

        this.$modal.msgSuccess("删除成功");
      })
        .catch(() => { });
    },
  },
};
</script>
